#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "low_speed_task.h"
#include "aht20.h"
#include "device_app.h"

#include "ohos_init.h"
#include "cmsis_os2.h"

low_speed_task_data_t low_speed_task_data;

extern device_data_t device_data;

void low_speed_task(void *arg)
{
    (void) arg;
    uint32_t retval = 0;
    float humidity = 0.0f;
    float temperature = 0.0f;
    while(1)
    {
        if(device_data.aht20_init_state == 1)
        {
            retval = AHT20_StartMeasure();
            if (retval != 0)
            {
                printf("trigger measure failed!\n");
            }
            retval = AHT20_GetMeasureResult(&temperature, &humidity);
            if (retval != 0)
            {
                printf("get humidity data failed!\n");
            }
            bzero(low_speed_task_data.temperature, sizeof(low_speed_task_data.temperature));
            sprintf(low_speed_task_data.temperature, "%2.2fC\0", temperature);
            bzero(low_speed_task_data.humidity, sizeof(low_speed_task_data.humidity));
            sprintf(low_speed_task_data.humidity, "%2.2f", humidity);
            strcat(low_speed_task_data.humidity, "%\0");
        }
        osDelay(500);   //5s
    }
}

void low_speed_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "low_speed_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 10240;
    attr.priority   = osPriorityNormal2;

    if (osThreadNew(low_speed_task, NULL, &attr) == NULL)
    {
        printf("[low_speed_task_creat] Falied to create low_speed_task!\n");
    }
}

APP_FEATURE_INIT(low_speed_task_creat);